package com.teleca.jamendo.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.teleca.jamendo.JamendoApplication;
import com.teleca.jamendo.R;

/**
 * 
 * @ClassName: AboutDialog
 * @Description: 关于界面对话框
 * @author zou.sq
 * @date 2013-5-30 下午5:54:24
 * 
 */
public class AboutDialog extends Dialog implements OnClickListener {

	private TextView mVersionTextView;
	private Button mTermsButton;
	private Button mCompanyButton;
	private Button mCancelButton;
	private ViewFlipper mCompanyViewFlipper;
	private Activity mContext;

	public AboutDialog(Activity context) {
		super(context);
		init(context);
	}

	public AboutDialog(Activity context, int theme) {
		super(context, theme);
		init(context);
	}

	public AboutDialog(Activity context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	/**
	 * Sharable code between constructors
	 */
	private void init(final Activity context) {
		mContext = context;
		setContentView(R.layout.about);

		String topText = "v " + JamendoApplication.getInstance().getVersion() + ", "
				+ context.getString(R.string.about_note);
		mVersionTextView = (TextView) findViewById(R.id.VersionText);
		mVersionTextView.setText(topText);
		mTermsButton = (Button) findViewById(R.id.TermsButton);
		mCompanyViewFlipper = (ViewFlipper) findViewById(R.id.CompanyViewFlipper);
		mCompanyButton = (Button) findViewById(R.id.AboutCompanyButton);
		mCancelButton = (Button) findViewById(R.id.CancelButton);
		setListener();
	}

	private void setListener() {
		mCancelButton.setOnClickListener(this);
		mTermsButton.setOnClickListener(this);
		mCompanyButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.AboutCompanyButton:
				int currentCompany = mCompanyViewFlipper.getDisplayedChild();
				if (currentCompany == 0) {
					mCompanyButton.setText(R.string.about_jamendo);
					mCompanyViewFlipper.setDisplayedChild(1);
				} else {
					mCompanyButton.setText(R.string.about_teleca);
					mCompanyViewFlipper.setDisplayedChild(0);
				}
				break;
			case R.id.CancelButton:
				AboutDialog.this.dismiss();
				break;
			case R.id.TermsButton:
				Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jamendo.com/en/cgu_user"));
				mContext.startActivity(myIntent);
				break;

			default:
				break;
		}

	}

}
